_realname=slim-simulator
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.0
pkgrel=1
pkgdesc="SLiM forward simulation software package for population genetics (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://messerlab.org/slim/"
license=("spdx:GPL-3.0-or-later")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-qt5-base")
source=("https://github.com/MesserLab/SLiM/releases/download/v${pkgver}/SLiM.zip"
        "0001-Remove-Qt-version-check.patch")
sha256sums=('8cb97d927c12c52c435f68ca03d507f4b02d7446d276493728c73bd1e6c130fc'
            '11679c3844de8d86b98f392ed1bf3711b15c7b5cc89eb2639c9e34fce3ede8c1')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

prepare() {
  cd SLiM

  apply_patch_with_msg \
    0001-Remove-Qt-version-check.patch
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_SLIMGUI=ON \
      ../SLiM

  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -Dm 644 "${srcdir}/SLiM/LICENSE" -t "${pkgdir}${MINGW_PREFIX}"/share/licenses/SLiM/
  install -Dm 644 "${srcdir}/SLiM/VERSIONS" "${srcdir}/SLiM"/README.html \
    -t "${pkgdir}${MINGW_PREFIX}"/share/doc/SLiM/
}
